<template>
<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
	<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
		<el-form-item label="描述" prop="title">
			<el-input v-model="form.title" placeholder="计划任务标题" clearable></el-input>
		</el-form-item>
		<el-form-item label="任务分类" prop="type">
			<el-select v-model="form.type" placeholder="计划任务执行类名称" clearable>
				<el-option v-for="item in [{value: 1, label: '执行命令'}, {value: 2, label: '执行类class'}, {value: 3, label: '执行地址'}, {value: 4, label: '执行shell'}]" :key="item.value" :label="item.label" :value="item.value" ></el-option>
			</el-select>
		</el-form-item>
		<el-form-item label="执行类" prop="command">
			<el-input v-model="form.command" placeholder="计划任务执行类名称" clearable></el-input>
		</el-form-item>
		<el-form-item label="定时规则" prop="expression">
			<sc-cron v-model="form.expression" placeholder="请输入Cron定时规则" clearable :shortcuts="shortcuts"></sc-cron>
		</el-form-item>
		<el-form-item label="是否启用" prop="status">
			<el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
		</el-form-item>
		<el-form-item label="备注" prop="remark">
			<el-input v-model="form.remark" placeholder="备注" clearable></el-input>
		</el-form-item>
	</el-form>
	<template #footer>
		<el-button @click="visible=false" >取 消</el-button>
		<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
	</template>
</el-dialog>
</template>

<script>
import scCron from '@/components/scCron';

export default {
	components: {
		scCron
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增计划任务',
				edit: '编辑计划任务'
			},
			form: {
				id:"",
				title: "",
				type: 1,
				command: "",
				expression: "",
				status: 1,
				remark: ""
			},
			rules: {
				title:[{required: true, message: '请填写标题'}],
				command:[{required: true, message: '请填写执行类'}],
				expression:[{required: true, message: '请填写定时规则'}]
			},
			visible: false,
			isSaveing: false,
			shortcuts: [
				{
					text: "每天8点和12点 (自定义追加)",
					value: "0 0 8,12 * * ?"
				}
			]
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					let res = await this.$API.system.crontab[this.mode].post(this.form)

					if(res.code == 1){
						this.isSaveing = false;
						this.visible = false;
						this.$message.success("操作成功")
						this.$emit('success', this.form, this.mode)
					}else{
						this.$message.error(res.message)
						this.isSaveing = false;
					}
				}
			})
		},
		//表单注入数据
		setData(data){
			this.form.id = data.id ?? 0
			this.form.title = data.title ?? ""
			this.form.type = data.type ?? 1
			this.form.command = data.command ?? ""
			this.form.expression = data.expression ?? ""
			this.form.status = data.status ?? 1
		}
	}
}
</script>

<style>
</style>
